<?php
class News_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    /*
	查询所有资讯
    */
    function GetNewsAllList()
    {
    	$this->load->database();
    	$sql = "SELECT * FROM news"; 

		$query = $this->db->query($sql);

		if ($query->num_rows() > 0)
		{
		   return $query->result_array(); 		 
		}
		
    }

    /**
    *  分页查询
    *  $pageindex页码,$pagesize页大小，$recordcount返回的总记录数
    */
    function GetNewsPagination($pageindex,$pagesize,&$recordcount)
    {
        if ($pageindex<1) $pageindex = 1; 
        $pagestart = ($pageindex - 1) * $pagesize;
        $this->load->library('mysqlidb');

        $sql = "SELECT SQL_CALC_FOUND_ROWS  * FROM news LIMIT ".$pagestart.",".$pagesize.";SELECT FOUND_ROWS() ;"; 

        $result = $this->mysqlidb->querypagination($sql);
        $recordcount = $result[1][0][0];
        //print_r($sql);
        return $result[0];
    }

    

    /*
    *查询指定父类下面的子类
    *$parentId 父类ID（int）
    */
    function GetCategoryListByParent($parentId)
    {
        $this->load->database();
        $sql = "SELECT * FROM newscategory WHERE ParentId = ?;"; 


        $query = $this->db->query($sql, array($parentId));

        if ($query->num_rows() > 0)
        {
           return $query->result_array();         
        }
    }


    /**
    * 获取实体
    */
    function GetEntity($id)
    {
        $this->load->database();
        $sql = "SELECT * FROM news WHERE NewsId = ? LIMIT 1;"; 


        $query = $this->db->query($sql, array($id));

        if ($query->num_rows() > 0)
        {
           return $query->row();         
        }
    }

    /**
    * 添加分类
    */
    function Insert($Title,$MetaTitle,$Image,$Summary,$CategoryId,$Url,$Content,$IsRecommend)
    {
        $data = array('Title' => $Title,'MetaTitle' => $MetaTitle,'Image' => $Image,'Summary' => $Summary,'CategoryId' => $CategoryId,'Url' => $Url,'Content' => $Content,'IsRecommend' => $IsRecommend,'CreateOn' => 'NOW()','CreateBy' => 'system','ModifyOn' => 'NOW()' );
        //$sql = "INSERT INTO newscategory (CategoryName,ParentId,Comment,CreateOn,CreateBy,ModifyOn) VALUES (?,?,?,NOW(),?,NOW());";
        $this->load->database();
        $result = $this->db->insert('news',$data);
        return $result;
    }

    /**
    * 修改分类
    */
    function Update($id,$Title,$MetaTitle,$Image,$Summary,$CategoryId,$Url,$Content,$IsRecommend)
    {
        $data = array('Title' => $Title,'MetaTitle' => $MetaTitle,'Image' => $Image,'Summary' => $Summary,'CategoryId' => $CategoryId,'Url' => $Url,'Content' => $Content,'IsRecommend' => $IsRecommend,'ModifyBy' => 'system','ModifyOn' => 'NOW()' );
        $where = array('NewsId' => $id );

        $this->load->database();
        $result = $this->db->update('news',$data,$where);
        return $result;
    }

    /**
    * 删除
    */
    function Delete($id)
    {
        $where = array('NewsId' => $id );
        $this->load->database();
        $result = $this->db->delete('news',$where);
        return $result;
    }
}

?>